﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QidorSpider.Components.Data;
using QidorSpider.Components.Util;
using System.Drawing.Imaging;

namespace QidorSpider
{
    public partial class FormKey : Form
    {
        public FormKey()
        {
            InitializeComponent();
        }

        private void btnUserd_Click(object sender, EventArgs e)
        {
            try
            {
                int keyID = int.Parse(treeViewKey.SelectedNode.Name);
                UpdateKeyData(keyID);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                int keyID = int.Parse(treeViewKey.SelectedNode.Name);

                // update base table



                this.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示");
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                btnUserd_Click(sender, e);

                this.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示");
            }
        }

        #region FormKey_Load
        /// <summary>
        /// form load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormKey_Load(object sender, EventArgs e)
        {
            try
            {
                // load treeview
                LoadTreeView();

                // load default key
                LoadKeyData(2);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示");
            }
        }
        #endregion

        #region LoadTreeView
        /// <summary>
        /// load treeview
        /// </summary>
        private void LoadTreeView()
        {
            try
            {
                List<ClassData> classList = SpiderDAL.GetClassDataList(Common.ClassType.Key);
                if(classList != null && classList.Count > 0)
                {
                    foreach(ClassData classData in classList)
                    {
                        TreeNode parentNode = new TreeNode();
                        parentNode.Name = "c" + classData.g_id.ToString();
                        parentNode.Text = classData.g_name;

                        List<KeyData> keyList = SpiderDAL.GetKeyDataList(classData.g_id, 0);

                        if(keyList != null && keyList.Count > 0)
                        {
                            foreach(KeyData key in keyList)
                            {
                                parentNode.Nodes.Add("k" + key.id.ToString(), key.name);
                            }
                        }

                        treeViewKey.Nodes.Add(parentNode);
                    }
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region LoadKeyData
        /// <summary>
        /// load key data
        /// </summary>
        /// <param name="keyID"></param>
        private void LoadKeyData(int keyID)
        {
            List<KeyData> keyDataList = SpiderDAL.GetKeyDataList(0, keyID);

            if(keyDataList != null && keyDataList.Count > 0)
            {
                KeyData keyData = keyDataList[0];

                txtKeyTitle.Text = keyData.key_title;
                txtMgTitle.Text = keyData.mg_title;
                txtMgContent.Text = keyData.mg_content;

                chkImgMark.Checked = keyData.imgmark;
                txtImgMarkFileName.Text = keyData.imgmarkfilename;

                if(keyData.imgwz == 1)
                    rboImgWz1.Checked = true;
                if(keyData.imgwz == 2)
                    rboImgWz2.Checked = true;
                if(keyData.imgwz == 3)
                    rboImgWz3.Checked = true;
                if(keyData.imgwz == 4)
                    rboImgWz4.Checked = true;
                if(keyData.imgwz == 5)
                    rboImgWz5.Checked = true;
                if(keyData.imgwz == 6)
                    rboImgWz6.Checked = true;
                if(keyData.imgwz == 7)
                    rboImgWz7.Checked = true;
                if(keyData.imgwz == 8)
                    rboImgWz8.Checked = true;
                if(keyData.imgwz == 9)
                    rboImgWz9.Checked = true;

                txtLogOh.Text = keyData.logoh.ToString();
                txtLogOw.Text = keyData.logow.ToString();
            }
        }
        #endregion

        private void UpdateKeyData(int keyID)
        {
            KeyData keyData = new KeyData();
            keyData.id = keyID;

            keyData.key_title = txtKeyTitle.Text;
            keyData.mg_title = txtMgTitle.Text;
            keyData.mg_content = txtMgContent.Text;

            keyData.imgmark = chkImgMark.Checked;
            keyData.imgmarkfilename = txtImgMarkFileName.Text;

            if(rboImgWz1.Checked)
                keyData.imgwz = 1;
            if(rboImgWz2.Checked)
                keyData.imgwz = 2;
            if(rboImgWz3.Checked)
                keyData.imgwz = 3;
            if(rboImgWz4.Checked)
                keyData.imgwz = 4;
            if(rboImgWz5.Checked)
                keyData.imgwz = 5;
            if(rboImgWz6.Checked)
                keyData.imgwz = 6;
            if(rboImgWz7.Checked)
                keyData.imgwz = 7;
            if(rboImgWz8.Checked)
                keyData.imgwz = 8;
            if(rboImgWz9.Checked)
                keyData.imgwz = 9;

            keyData.logoh = double.Parse(txtLogOh.Text);
            keyData.logow = double.Parse(txtLogOw.Text);

            SpiderDAL.UpdKeyData(keyData);
        }

        private Bitmap changeOpacity(string imgMarkFileName, float opacity)
        {
            float[][] nArray ={ new float[] {1, 0, 0, 0, 0},
            new float[] {0, 1, 0, 0, 0},
            new float[] {0, 0, 1, 0, 0},
            new float[] {0, 0, 0, opacity, 0},
            new float[] {0, 0, 0, 0, 1}};
 
            ColorMatrix matrix = new ColorMatrix(nArray);
            ImageAttributes attributes = new ImageAttributes();
            attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

            Image srcImage = Image.FromFile(imgMarkFileName);

            Bitmap resultImage = new Bitmap(srcImage.Width, srcImage.Height);

            Graphics g = Graphics.FromImage(resultImage);

            g.DrawImage( srcImage, new Rectangle( 0, 0, srcImage.Width, srcImage.Height ), 0, 0, srcImage.Width, srcImage.Height, GraphicsUnit.Pixel, attributes);

            return resultImage;
        }

    }
}
